package com.gnomon;

import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.Company;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.RoleServiceUtil;
import com.liferay.portal.util.PortalUtil;

public class LiferayUtils {

	private static Logger log = Logger.getLogger("LiferayUtils");
	
	private static PortletRequest getPortletRequest() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest) externalContext
				.getRequest();
		return portletRequest;
	}

	public static void storeToSession(String param, Object value) {
		try
		{
	    log.info("Try to store to session the parameter : " + param + " with value : " + value);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest) externalContext
				.getRequest();
		HttpServletRequest req = PortalUtil.getHttpServletRequest(portletRequest);
		HttpSession session = req.getSession();
		session.putValue(param, value);
		}
		catch (Exception e)
		{
	    log.error("ERROR: While trying to store to session the parameter : " + param + " with value : " + value + ": " + e.getMessage());			
		}
	}

	public static Object getFromSession(String param) {
		Object ret = null;
		try
		{
	    log.info("Try to get from session the parameter : " + param);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest) externalContext
				.getRequest();
		HttpServletRequest req = PortalUtil.getHttpServletRequest(portletRequest);
		HttpSession session = req.getSession();
		ret = session.getAttribute(param);
		}
		catch (Exception e)
		{
		log.error("ERROR: While trying to get from session the parameter : " + param + ": " + e.getMessage());
		}
		return ret;
	}

	
	public static boolean isPharmacist(long userid,long companyid)
	{
	return userHasRole(userid,companyid,"Pharmacist");	
	}

	public static boolean isDoctor(long userid,long companyid)
	{
	return userHasRole(userid,companyid,"Doctor");	
	}

	public static boolean isNurse(long userid,long companyid)
	{
	return userHasRole(userid,companyid,"Nurse");	
	}
	
	public static boolean isAdministrator(long userid,long companyid)
	{
	return userHasRole(userid,companyid,"Administrator");	
	}
	
	public static boolean userHasRole(long userId,long companyId, String rolename)
	{
		boolean hasRole = false;
		try {
			hasRole = RoleLocalServiceUtil.hasUserRole(userId, companyId, rolename, false);
			if (hasRole) log.info("User has role: " + rolename);
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		try {
//			List<Role> userRoles = RoleServiceUtil. getUserRoles(userid);
//			
//			for (int i=0;i<userRoles.size();i++)	{
//				Role role = userRoles.get(i);
//				log.info("ROLE: " + role.getName());
//				if (role.getName().equalsIgnoreCase(rolename))
//					return true;
//			}
//		} catch (PortalException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (SystemException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return hasRole;
	}
	
	public static User getPortalUser() {
		User user = null;
		PortletRequest portletRequest = getPortletRequest();
		try {
			user = PortalUtil.getUser(portletRequest);
		} catch (PortalException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SystemException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return user;
	}

	public static Company getPortalCompany() {
		Company company = null;
		PortletRequest portletRequest = getPortletRequest();
		try {
			company = PortalUtil.getCompany(portletRequest);
		} catch (PortalException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SystemException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return company;
	}
	
	public static String getPortalLanguage() {
		PortletRequest portletRequest = getPortletRequest();
		String lang = portletRequest.getLocale().getLanguage() + "-"
				+ portletRequest.getLocale().getCountry();
		return lang;
	}

}
